﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TeamCommunitySolution.Domain.DataLayer.Base;
using TeamCommunitySolution.Models;
using TeamCommunitySolution.Modules.Base;
using TeamCommunitySolution.Modules.Base.Requests;
using TeamCommunitySolution.Modules.Base.Responses;
using TeamCommunitySolution.Modules.Shared;
using TeamCommunitySolution.Repositories.Base;

namespace TeamCommunitySolution.Extensions.ModuleProxies
{
    /// <summary>
    /// Base Module Proxy
    /// </summary>
    public class BaseModuleProxy
    {
        /// <summary>
        /// Registers the user.
        /// </summary>
        /// <param name="model">The model.</param>
        internal void RegisterUser(RegistrationModel model)
        {
            try
            {
                RegistrationRequest request = new RegistrationRequest();
                request.Email = model.EMail;
                request.FirstName = model.FirstName;
                request.LastName = model.LastName;
                request.Password = model.Password;
                request.NickName = model.NickName;

                RegistrationResponse response = BaseManagerInstance().RegisterUser(request);

                if (response.Status != OperationStatus.Success)
                {
                    throw new Exception("Unable to complete registration: ", response.Exception);
                }
            }
            catch (Exception e)
            {
                throw new Exception("Unable to complete registration", e);
            }
        }

        /// <summary>
        /// Finds the user by username.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <returns>A user model.</returns>
        internal UserModel FindUserByUsername(string username)
        {
            SystemUserRequest request = new SystemUserRequest();
            request.Username = username;

            SystemUser user = BaseManagerInstance().GetSystemUserByUsername(request).SystemUser;
            UserModel output = new UserModel();

            if (user != null)
            {
                output.FirstName = user.FirstName;
                output.LastName = user.LastName;
                output.NickName = user.NickName;
            }

            return output;
        }

        /// <summary>
        /// Finds the name of the user by nick.
        /// </summary>
        /// <param name="nick">The nick.</param>
        /// <returns>User Model</returns>
        internal UserModel FindUserByNickName(string nick)
        {
            SystemUserRequest request = new SystemUserRequest();
            request.Username = nick;

            SystemUser user = BaseManagerInstance().GetSystemUserByNickname(request).SystemUser;
            UserModel output = new UserModel();

            if (user != null)
            {
                output.FirstName = user.FirstName;
                output.LastName = user.LastName;
                output.NickName = user.NickName;
            }

            return output;
        }

        /// <summary>
        /// Bases the manager instance.
        /// </summary>
        /// <returns>Base Manager</returns>
        private BaseManager BaseManagerInstance()
        {
            return BaseManagerImpl.CreateInstance(BaseRepositoryImpl.CreateInstance());
        }
    }
}
